package com.cz.nongyetongv5.activity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.adapter.WheelViewAdapter;
import com.cz.nongyetongv5.adapter.config.CommonConstants;
import com.cz.nongyetongv5.adapter.dao.DatabaseHelper;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.JianChaHangYe;
import com.cz.nongyetongv5.bean.JianChaNeiRong;
import com.cz.nongyetongv5.bean.JianGuanDuiXiangFilter;
import com.cz.nongyetongv5.bean.TextInfo;
import com.cz.nongyetongv5.bean.User;
import com.cz.nongyetongv5.custom.dialog.CustomDialog;
import com.cz.nongyetongv5.custom.wheelview.Utils;
import com.cz.nongyetongv5.custom.wheelview.WheelView;
import com.cz.nongyetongv5.utils.CharsetUtil;
import com.cz.nongyetongv5.utils.ConfigUtil;
import com.cz.nongyetongv5.utils.DialogUtil;
import com.cz.nongyetongv5.utils.HttpUtils;
import com.cz.nongyetongv5.utils.HttpUtils.OnRequestExecuted;

public class JianGuanDuiXiangFilterActivity extends Activity implements
		OnClickListener {

	/**
	 * 企业性质
	 */
	private ViewGroup rl_jianguanduixiang_xingzhi = null;

	/**
	 * 企业类别
	 */
	private ViewGroup rl_jianguanduixiang_leibie = null;

	/**
	 * 三品类型
	 */
	private ViewGroup rl_jianguanduixiang_leixing = null;

	private ViewGroup rl_jianguanduixiang_xinxi = null;

	private ViewGroup rl_jianguanduixiang_year = null;

	private DatabaseHelper db = null;

	private JianGuanDuiXiangFilter filter = new JianGuanDuiXiangFilter();

	private ViewGroup btnBack = null;

	private ViewGroup btnSaveFilter = null;

	private Button btnClearFilter = null;

	private User user;

	@Override
	protected void onDestroy() {

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().remove(this);
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().add(this);

		setContentView(R.layout.jianguanduixiang_filter);

		filter = (JianGuanDuiXiangFilter) getIntent().getSerializableExtra(
				"filter");

		db = new DatabaseHelper(this, CommonConstants.DBNAME, null,
				CommonConstants.DATABASE_VERSION);

		btnClearFilter = (Button) findViewById(R.id.btnClearFilter);

		rl_jianguanduixiang_xingzhi = (ViewGroup) findViewById(R.id.rl_jianguanduixiang_xingzhi);

		rl_jianguanduixiang_leibie = (ViewGroup) findViewById(R.id.rl_jianguanduixiang_leibie);

		rl_jianguanduixiang_leixing = (ViewGroup) findViewById(R.id.rl_jianguanduixiang_leixing);

		rl_jianguanduixiang_xinxi = (ViewGroup) findViewById(R.id.rl_jianguanduixiang_xinxi);

		rl_jianguanduixiang_year = (ViewGroup) findViewById(R.id.rl_jianguanduixiang_year);

		rl_jianguanduixiang_xingzhi.setOnClickListener(this);

		rl_jianguanduixiang_leibie.setOnClickListener(this);

		rl_jianguanduixiang_leixing.setOnClickListener(this);

		rl_jianguanduixiang_xinxi.setOnClickListener(this);

		rl_jianguanduixiang_year.setOnClickListener(this);

		btnBack = (ViewGroup) findViewById(R.id.btnBack);

		btnBack.setOnClickListener(this);

		btnSaveFilter = (ViewGroup) findViewById(R.id.btnSaveFilter);

		btnSaveFilter.setOnClickListener(this);

		btnClearFilter.setOnClickListener(this);

		changeIcon(rl_jianguanduixiang_xingzhi);
		changeIcon(rl_jianguanduixiang_leibie);
		changeIcon(rl_jianguanduixiang_leixing);
		changeIcon(rl_jianguanduixiang_xinxi);
		changeIcon(rl_jianguanduixiang_year);

		user = (User) getIntent().getSerializableExtra("user");

	}

	@Override
	public void onClick(View v) {

		if (btnClearFilter == v) {

			filter = new JianGuanDuiXiangFilter();

			onClick(btnSaveFilter);

			return;
		}

		if (v == btnSaveFilter) {

			new AsyncTask<Integer, Integer, Integer>() {

				@Override
				protected void onPreExecute() {

					if (filter.getLeixing().size() > 0
							|| filter.getXinxi().size() > 0) {
						CustomDialog wait = new CustomDialog(
								JianGuanDuiXiangFilterActivity.this);

						wait.setMessage("正在查询..");

						wait.showWait();
					}

				}

				@Override
				protected Integer doInBackground(Integer... params) {

					HttpUtils httpUtils = HttpUtils.getInstance();

					httpUtils.setOnRequestExecuted(new OnRequestExecuted() {

						@Override
						public void onRequestExecuted(int responseCode,
													  String responseBody) {

							if (responseCode == HttpUtils.SUCCESS) {

								try {
									JSONObject jsonObject = new JSONObject(
											responseBody);

									String status = jsonObject
											.getString("status");

									if ("true".equals(status)) {

										filter.getIds().clear();

										JSONArray jsonArray = jsonObject
												.getJSONArray("result");

										for (int i = 0; i < jsonArray.length(); i++) {

											JSONObject objectItem = jsonArray
													.getJSONObject(i);

											String id = objectItem
													.getString("id");

											filter.getIds().add(id);

										}

									}

								} catch (JSONException e) {
								}
							}

						}
					});

					String requestUrl = new ConfigUtil(getResources())
							.getPreHttpUrl()
							+ getString(R.string.jianguanduixiang_filter_url);

					StringBuffer leixing = new StringBuffer();

					StringBuffer xinxi = new StringBuffer();

					if (filter.getLeixing().size() > 0) {

						requestUrl += "cert_categories/";

						for (Integer integer : filter.getLeixing()) {

							leixing.append(integer);

							leixing.append(",");

						}

						if (leixing.length() > 1) {

							requestUrl += leixing.substring(0,
									leixing.length() - 1);

							requestUrl += "/";
						}

					}

					if (filter.getXinxi().size() > 0) {

						requestUrl += "warning/";

						for (Integer integer : filter.getXinxi()) {

							xinxi.append(integer);

							xinxi.append(",");

						}

						if (xinxi.length() > 0) {

							requestUrl += xinxi
									.substring(0, xinxi.length() - 1);

							requestUrl += "/";
						}

					}

					requestUrl += "year/" + filter.getYear();

					String authUserName = new ConfigUtil(getResources())
							.getRestUsername();

					String authPassword = new ConfigUtil(getResources())
							.getRestPassword();

					if (filter.getLeixing().size() > 0
							|| filter.getXinxi().size() > 0) {

						httpUtils.sendRequest(requestUrl, null, "GET", "utf-8",
								authUserName, authPassword, user);

					}

					return null;
				}

				@Override
				protected void onPostExecute(Integer result) {

					Intent intent = new Intent();

					intent.putExtra("filter", filter);

					setResult(JianGuanDuiXiangActivity.REQUEST_FILTER_CODE,
							intent);

					finish();

				}

			}.execute();

			return;
		}

		if (v == btnBack) {

			finish();

			return;

		}

		// 初始化底部弹出的窗口
		final AlertDialog bottomWindow = new AlertDialog.Builder(
				new ContextThemeWrapper(JianGuanDuiXiangFilterActivity.this,
						R.style.NongYeTongFullscreenTheme)).create();

		// 输入法的设置
		bottomWindow.getWindow().setFlags(
				WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
				WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

		// 设置窗口的动画
		bottomWindow.getWindow().setWindowAnimations(R.style.bottomAnimStyle);

		// 此处可以设置dialog显示的位置 ,显示在底部
		bottomWindow.getWindow().setGravity(Gravity.BOTTOM);

		bottomWindow.show();

		// 弹出输入法的操作
		bottomWindow.getWindow().clearFlags(
				WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

		ViewGroup bottomWindowLayout = null;

		if (v == rl_jianguanduixiang_year) {

			bottomWindowLayout = (LinearLayout) getLayoutInflater().inflate(
					R.layout.bottom_jianchaneirong, null);

			LinearLayout root = (LinearLayout) bottomWindowLayout
					.findViewById(R.id.jianchaneironglayout);

			final WheelView wheelView = new WheelView(
					JianGuanDuiXiangFilterActivity.this);

			final WheelViewAdapter adpWheelView = new WheelViewAdapter(
					JianGuanDuiXiangFilterActivity.this);

			ArrayList<TextInfo> data = new ArrayList<TextInfo>();

			Calendar calendar = new GregorianCalendar();

			for (int i = calendar.get(Calendar.YEAR); i >= 2013; i--) {
				data.add(new TextInfo(i, i + ""));
			}

			adpWheelView.setData(data);

			wheelView.setAdapter(adpWheelView);

			int height = Utils.dip2px(getApplicationContext(), 300);

			root.addView(wheelView, new LayoutParams(LayoutParams.MATCH_PARENT,
					height));

			TextView tvTitle = (TextView) bottomWindowLayout
					.findViewById(R.id.bottomTitle);

			tvTitle.setText(getString(R.string.jianguanduixiang_shijian));

			final ViewGroup btnBottomBack = (ViewGroup) bottomWindowLayout
					.findViewById(R.id.btnBack);

			final ViewGroup btnSureJianChaNeiRong = (ViewGroup) bottomWindowLayout
					.findViewById(R.id.btnSureJianChaNeiRong);

			wheelView.setSelection(0);

			if (CharsetUtil.isNotNull(filter.getYear())) {
				int index = data.indexOf(new TextInfo(Integer.parseInt(filter
						.getYear()), filter.getYear()));

				if (index > -1) {
					wheelView.setSelection(index);
				}

			}

			OnClickListener onClickListener = new OnClickListener() {

				@Override
				public void onClick(View v) {

					if (v == btnBottomBack) {

						bottomWindow.dismiss();
					}

					TextInfo textInfo = (TextInfo) wheelView.getSelectedItem();

					filter.setYear(textInfo.mText);

					changeIcon(rl_jianguanduixiang_year);

					bottomWindow.dismiss();

				}
			};

			btnBottomBack.setOnClickListener(onClickListener);
			btnSureJianChaNeiRong.setOnClickListener(onClickListener);

		}

		if (rl_jianguanduixiang_xingzhi == v) {

			List<JianGuanDuiXiangFilter> filters = new ArrayList<JianGuanDuiXiangFilter>();
			filters.add(new JianGuanDuiXiangFilter(1, "企业"));
			filters.add(new JianGuanDuiXiangFilter(2, "合作社"));
			filters.add(new JianGuanDuiXiangFilter(3, "行业协会"));
			filters.add(new JianGuanDuiXiangFilter(4, "其他"));

			bottomWindowLayout = createViewByData(filters,
					"rl_jianguanduixiang_xingzhi");

			TextView tvTitle = (TextView) bottomWindowLayout
					.findViewById(R.id.bottomTitle);

			tvTitle.setText(getString(R.string.jianguanduixiang_xingzhi));

			final ViewGroup btnBottomBack = (ViewGroup) bottomWindowLayout
					.findViewById(R.id.btnBack);

			final ViewGroup btnSureJianChaNeiRong = (ViewGroup) bottomWindowLayout
					.findViewById(R.id.btnSureJianChaNeiRong);

			OnClickListener onClickListener = new OnClickListener() {

				@Override
				public void onClick(View v) {

					if (v == btnBottomBack) {

						bottomWindow.dismiss();
					}

					if (v == btnSureJianChaNeiRong) {

						// 增加条件

						LinearLayout root = (LinearLayout) bottomWindow
								.findViewById(R.id.jianchaneironglayout);

						filter.getKinds().clear();

						for (int i = 0; i < root.getChildCount(); i++) {

							ViewGroup viewGroup = (ViewGroup) root
									.getChildAt(i);

							if (viewGroup instanceof RelativeLayout) {

								for (int j = 0; j < viewGroup.getChildCount(); j++) {

									View view = viewGroup.getChildAt(j);

									if (view instanceof CheckBox) {

										CheckBox cb = (CheckBox) view;

										if (cb.isChecked()) {

											JianGuanDuiXiangFilter duiXiangFilter = (JianGuanDuiXiangFilter) cb
													.getTag();

											filter.getKinds().add(
													duiXiangFilter.getId());

										}
									}

								}

							}

						}

						changeIcon(rl_jianguanduixiang_xingzhi);

						bottomWindow.dismiss();
					}

				}
			};

			btnBottomBack.setOnClickListener(onClickListener);
			btnSureJianChaNeiRong.setOnClickListener(onClickListener);

		}

		if (rl_jianguanduixiang_leibie == v) {

			List<JianGuanDuiXiangFilter> filters = new ArrayList<JianGuanDuiXiangFilter>();

			List<JianChaHangYe> chaHangYes = db.queryAllJianChaHangYe();

			for (JianChaHangYe jianChaHangYe : chaHangYes) {
				filters.add(new JianGuanDuiXiangFilter(jianChaHangYe.getId(),
						jianChaHangYe.getName()));

			}
			chaHangYes.clear();
			chaHangYes = null;

			bottomWindowLayout = createViewByData(filters,
					"rl_jianguanduixiang_leibie");

			TextView tvTitle = (TextView) bottomWindowLayout
					.findViewById(R.id.bottomTitle);

			tvTitle.setText(getString(R.string.jianguanduixiang_leibie));

			final ViewGroup btnBottomBack = (ViewGroup) bottomWindowLayout
					.findViewById(R.id.btnBack);

			final ViewGroup btnSureJianChaNeiRong = (ViewGroup) bottomWindowLayout
					.findViewById(R.id.btnSureJianChaNeiRong);

			OnClickListener onClickListener = new OnClickListener() {

				@Override
				public void onClick(View v) {

					if (v == btnBottomBack) {

						bottomWindow.dismiss();
					}

					if (v == btnSureJianChaNeiRong) {

						// 增加条件

						LinearLayout root = (LinearLayout) bottomWindow
								.findViewById(R.id.jianchaneironglayout);

						filter.getCategory().clear();

						for (int i = 0; i < root.getChildCount(); i++) {

							ViewGroup viewGroup = (ViewGroup) root
									.getChildAt(i);

							if (viewGroup instanceof RelativeLayout) {

								for (int j = 0; j < viewGroup.getChildCount(); j++) {

									View view = viewGroup.getChildAt(j);

									if (view instanceof CheckBox) {

										CheckBox cb = (CheckBox) view;

										if (cb.isChecked()) {

											JianGuanDuiXiangFilter duiXiangFilter = (JianGuanDuiXiangFilter) cb
													.getTag();

											filter.getCategory().add(
													duiXiangFilter.getId());

										}
									}

								}

							}

						}

						changeIcon(rl_jianguanduixiang_leibie);

						bottomWindow.dismiss();
					}

				}
			};

			btnBottomBack.setOnClickListener(onClickListener);
			btnSureJianChaNeiRong.setOnClickListener(onClickListener);

		}

		/**
		 * 三品证书已过期 三品证书j到期
		 *
		 */
		if (rl_jianguanduixiang_leixing == v) {

			if (isFirstPlay("rl_jianguanduixiang_leixing")) {

				DialogUtil.showMessageDialog("注 : 无网状态下自动忽略此条件", null,
						JianGuanDuiXiangFilterActivity.this);

			}

			List<JianGuanDuiXiangFilter> filters = new ArrayList<JianGuanDuiXiangFilter>();

			List<JianChaNeiRong> neiRongs = db.queryAllJianChaNeiRong();

			for (JianChaNeiRong jianChaNeiRong : neiRongs) {

				if ("日常监管".equals(jianChaNeiRong.getName())) {

					continue;
				}

				filters.add(new JianGuanDuiXiangFilter(jianChaNeiRong.getId(),
						jianChaNeiRong.getName()));

			}

			neiRongs.clear();

			neiRongs = null;

			bottomWindowLayout = createViewByData(filters,
					"rl_jianguanduixiang_leixing");

			TextView tvTitle = (TextView) bottomWindowLayout
					.findViewById(R.id.bottomTitle);

			tvTitle.setText(getString(R.string.jianguanduixiang_leixing));

			final ViewGroup btnBottomBack = (ViewGroup) bottomWindowLayout
					.findViewById(R.id.btnBack);

			final ViewGroup btnSureJianChaNeiRong = (ViewGroup) bottomWindowLayout
					.findViewById(R.id.btnSureJianChaNeiRong);

			OnClickListener onClickListener = new OnClickListener() {

				@Override
				public void onClick(View v) {

					if (v == btnBottomBack) {

						bottomWindow.dismiss();
					}

					if (v == btnSureJianChaNeiRong) {

						// 增加条件

						LinearLayout root = (LinearLayout) bottomWindow
								.findViewById(R.id.jianchaneironglayout);

						filter.getLeixing().clear();

						for (int i = 0; i < root.getChildCount(); i++) {

							ViewGroup viewGroup = (ViewGroup) root
									.getChildAt(i);

							if (viewGroup instanceof RelativeLayout) {

								for (int j = 0; j < viewGroup.getChildCount(); j++) {

									View view = viewGroup.getChildAt(j);

									if (view instanceof CheckBox) {

										CheckBox cb = (CheckBox) view;

										if (cb.isChecked()) {

											JianGuanDuiXiangFilter duiXiangFilter = (JianGuanDuiXiangFilter) cb
													.getTag();

											filter.getLeixing().add(
													duiXiangFilter.getId());

										}
									}

								}

							}

						}

						changeIcon(rl_jianguanduixiang_leixing);

						bottomWindow.dismiss();
					}

				}
			};

			btnBottomBack.setOnClickListener(onClickListener);
			btnSureJianChaNeiRong.setOnClickListener(onClickListener);

		}

		if (v == rl_jianguanduixiang_xinxi) {

			if (isFirstPlay("rl_jianguanduixiang_xinxi")) {

				DialogUtil.showMessageDialog("注 : 无网状态下自动忽略此条件", null,
						JianGuanDuiXiangFilterActivity.this);

			}

			List<JianGuanDuiXiangFilter> filters = new ArrayList<JianGuanDuiXiangFilter>();
			filters.add(new JianGuanDuiXiangFilter(1, "三品一标 已过期"));
			filters.add(new JianGuanDuiXiangFilter(2, "产地信息 已过期"));
			filters.add(new JianGuanDuiXiangFilter(3, "三品一标 将到期"));
			filters.add(new JianGuanDuiXiangFilter(4, "产地信息 将到期"));
			filters.add(new JianGuanDuiXiangFilter(5, "监督检查 不合格"));
			filters.add(new JianGuanDuiXiangFilter(6, "产品检测 不合格"));

			bottomWindowLayout = createViewByData(filters,
					"rl_jianguanduixiang_xinxi");

			TextView tvTitle = (TextView) bottomWindowLayout
					.findViewById(R.id.bottomTitle);

			tvTitle.setText(getString(R.string.jianguanduixiang_leixing));

			final ViewGroup btnBottomBack = (ViewGroup) bottomWindowLayout
					.findViewById(R.id.btnBack);

			final ViewGroup btnSureJianChaNeiRong = (ViewGroup) bottomWindowLayout
					.findViewById(R.id.btnSureJianChaNeiRong);

			OnClickListener onClickListener = new OnClickListener() {

				@Override
				public void onClick(View v) {

					if (v == btnBottomBack) {

						bottomWindow.dismiss();
					}

					if (v == btnSureJianChaNeiRong) {

						// 增加条件

						LinearLayout root = (LinearLayout) bottomWindow
								.findViewById(R.id.jianchaneironglayout);

						filter.getXinxi().clear();

						for (int i = 0; i < root.getChildCount(); i++) {

							ViewGroup viewGroup = (ViewGroup) root
									.getChildAt(i);

							if (viewGroup instanceof RelativeLayout) {

								for (int j = 0; j < viewGroup.getChildCount(); j++) {

									View view = viewGroup.getChildAt(j);

									if (view instanceof CheckBox) {

										CheckBox cb = (CheckBox) view;

										if (cb.isChecked()) {

											JianGuanDuiXiangFilter duiXiangFilter = (JianGuanDuiXiangFilter) cb
													.getTag();

											filter.getXinxi().add(
													duiXiangFilter.getId());

										}
									}

								}

							}

						}

						changeIcon(rl_jianguanduixiang_xinxi);

					}

					bottomWindow.dismiss();

				}
			};

			btnBottomBack.setOnClickListener(onClickListener);
			btnSureJianChaNeiRong.setOnClickListener(onClickListener);

		}

		if (bottomWindowLayout != null) {

			bottomWindowLayout.setMinimumWidth(getWindowManager()
					.getDefaultDisplay().getWidth());

			// 设置底部布局
			bottomWindow.setContentView(bottomWindowLayout);
		}

	}

	/**
	 * 选择框的样式
	 *
	 * @param filters
	 * @param isMoreSelect
	 * @return
	 */
	private ViewGroup createViewByData(List<JianGuanDuiXiangFilter> filters,
									   String flag) {

		LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(
				R.layout.bottom_jianchaneirong, null);

		final LinearLayout root = (LinearLayout) linearLayout
				.findViewById(R.id.jianchaneironglayout);

		for (JianGuanDuiXiangFilter jianGuanDuiXiangFilter : filters) {

			RelativeLayout rootLayout = new RelativeLayout(
					JianGuanDuiXiangFilterActivity.this);

			rootLayout.setTag(jianGuanDuiXiangFilter);

			MarginLayoutParams rootLayoutPrams = new MarginLayoutParams(
					MarginLayoutParams.MATCH_PARENT,
					MarginLayoutParams.WRAP_CONTENT);

			rootLayoutPrams.setMargins(0,
					Utils.dip2px(JianGuanDuiXiangFilterActivity.this, 3), 0,
					Utils.dip2px(JianGuanDuiXiangFilterActivity.this, 3));

			// 兼容低版本设置margin方法
			LayoutParams layoutPrarams = new LayoutParams(rootLayoutPrams);
			layoutPrarams.gravity = Gravity.TOP | Gravity.LEFT;

			rootLayout.setLayoutParams(layoutPrarams);
			rootLayout.setClickable(true);
			rootLayout.setGravity(Gravity.CENTER_VERTICAL);
			rootLayout.setPadding(
					Utils.dip2px(JianGuanDuiXiangFilterActivity.this, 8),
					Utils.dip2px(JianGuanDuiXiangFilterActivity.this, 5),
					Utils.dip2px(JianGuanDuiXiangFilterActivity.this, 8),
					Utils.dip2px(JianGuanDuiXiangFilterActivity.this, 5));
			rootLayout
					.setBackgroundResource(R.drawable.jiandujiancha_item_style);

			/*********************************************/
			TextView tvTextView = new TextView(
					JianGuanDuiXiangFilterActivity.this);

			RelativeLayout.LayoutParams tvBeiJianChaHangYeLayout = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT,
					RelativeLayout.LayoutParams.WRAP_CONTENT);
			tvBeiJianChaHangYeLayout.addRule(RelativeLayout.CENTER_VERTICAL);
			tvTextView.setLayoutParams(tvBeiJianChaHangYeLayout);

			tvTextView.setPadding(
					Utils.dip2px(JianGuanDuiXiangFilterActivity.this, 8),
					Utils.dip2px(JianGuanDuiXiangFilterActivity.this, 8),
					Utils.dip2px(JianGuanDuiXiangFilterActivity.this, 8),
					Utils.dip2px(JianGuanDuiXiangFilterActivity.this, 8));
			tvTextView.setTextColor(Color.parseColor("#656769"));
			tvTextView.setTextSize(20);
			tvTextView.setText(jianGuanDuiXiangFilter.getName());

			rootLayout.addView(tvTextView);

			/*********************************************/

			final CheckBox cbCheckBox = new CheckBox(
					JianGuanDuiXiangFilterActivity.this);

			cbCheckBox.setTag(jianGuanDuiXiangFilter);

			RelativeLayout.LayoutParams cbBeiJianChaHangYeLayout = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT,
					RelativeLayout.LayoutParams.WRAP_CONTENT);
			cbBeiJianChaHangYeLayout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
			cbBeiJianChaHangYeLayout.addRule(RelativeLayout.CENTER_VERTICAL);
			cbCheckBox.setLayoutParams(cbBeiJianChaHangYeLayout);
			cbCheckBox.setChecked(false);
			cbCheckBox.setFocusable(false);

			cbCheckBox.setButtonDrawable(R.drawable.checkbox_select_style);

			if ("rl_jianguanduixiang_xingzhi".equals(flag)
					&& filter.getKinds().contains(
					jianGuanDuiXiangFilter.getId())) {
				cbCheckBox.setChecked(true);
			}

			if ("rl_jianguanduixiang_leibie".equals(flag)
					&& filter.getCategory().contains(
					jianGuanDuiXiangFilter.getId())) {
				cbCheckBox.setChecked(true);
			}

			if ("rl_jianguanduixiang_leixing".equals(flag)
					&& filter.getLeixing().contains(
					jianGuanDuiXiangFilter.getId())) {
				cbCheckBox.setChecked(true);
			}

			if ("rl_jianguanduixiang_xinxi".equals(flag)
					&& filter.getXinxi().contains(
					jianGuanDuiXiangFilter.getId())) {
				cbCheckBox.setChecked(true);
			}

			rootLayout.addView(cbCheckBox);

			// 绑定事件,当布局被点击的时候选中checkbox
			rootLayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					// 如果是多选的
					cbCheckBox.setChecked(!cbCheckBox.isChecked());

				}
			});

			root.addView(rootLayout);
		}

		return linearLayout;

	}

	/**
	 * 检测是不是第一次愉快的玩耍
	 *
	 * @return 是不是处
	 */
	private boolean isFirstPlay(String param) {

		SharedPreferences nongYeTongSharedPreferences = this
				.getSharedPreferences(CommonConstants.SHARED_REFERENCES_TAG,
						Context.MODE_PRIVATE);

		boolean isFirst = nongYeTongSharedPreferences.getBoolean(param, true);

		// 如果还是个处,勉强上了她
		if (isFirst == true) {

			Editor editor = nongYeTongSharedPreferences.edit();

			editor.putBoolean(param, false);

			// 插入
			editor.commit();

		}

		return isFirst;

	}

	private void changeIcon(View v) {

		if (rl_jianguanduixiang_xingzhi == v) {

			ImageView ivright = (ImageView) rl_jianguanduixiang_xingzhi
					.findViewWithTag("ivright");

			Animation imageFadeIn = AnimationUtils.loadAnimation(
					JianGuanDuiXiangFilterActivity.this,
					R.anim.icon_anim_move_x);

			if (filter.getKinds().size() > 0) {
				ivright.setImageResource(R.drawable.item_select);
			} else {
				ivright.setImageResource(R.drawable.jiandujiancha_icon_more);

			}

			// 加载进入
			ivright.clearAnimation();

			ivright.setAnimation(imageFadeIn);

		}

		if (rl_jianguanduixiang_leibie == v) {

			ImageView ivright = (ImageView) rl_jianguanduixiang_leibie
					.findViewWithTag("ivright");

			Animation imageFadeIn = AnimationUtils.loadAnimation(
					JianGuanDuiXiangFilterActivity.this,
					R.anim.icon_anim_move_x);

			if (filter.getCategory().size() > 0) {
				ivright.setImageResource(R.drawable.item_select);
			} else {
				ivright.setImageResource(R.drawable.jiandujiancha_icon_more);

			}

			// 加载进入
			ivright.clearAnimation();

			ivright.setAnimation(imageFadeIn);
		}

		if (rl_jianguanduixiang_leixing == v) {
			ImageView ivright = (ImageView) rl_jianguanduixiang_leixing
					.findViewWithTag("ivright");

			Animation imageFadeIn = AnimationUtils.loadAnimation(
					JianGuanDuiXiangFilterActivity.this,
					R.anim.icon_anim_move_x);

			if (filter.getLeixing().size() > 0) {
				ivright.setImageResource(R.drawable.item_select);
			} else {
				ivright.setImageResource(R.drawable.jiandujiancha_icon_more);

			}

			// 加载进入
			ivright.clearAnimation();

			ivright.setAnimation(imageFadeIn);
		}

		if (rl_jianguanduixiang_xinxi == v) {
			ImageView ivright = (ImageView) rl_jianguanduixiang_xinxi
					.findViewWithTag("ivright");

			Animation imageFadeIn = AnimationUtils.loadAnimation(
					JianGuanDuiXiangFilterActivity.this,
					R.anim.icon_anim_move_x);

			if (filter.getXinxi().size() > 0) {
				ivright.setImageResource(R.drawable.item_select);
			} else {
				ivright.setImageResource(R.drawable.jiandujiancha_icon_more);

			}

			// 加载进入
			ivright.clearAnimation();

			ivright.setAnimation(imageFadeIn);
		}

		if (rl_jianguanduixiang_year == v) {

			ImageView ivright = (ImageView) rl_jianguanduixiang_year
					.findViewWithTag("ivright");

			Animation imageFadeIn = AnimationUtils.loadAnimation(
					JianGuanDuiXiangFilterActivity.this,
					R.anim.icon_anim_move_x);

			ivright.setImageResource(R.drawable.item_select);

			// 加载进入
			ivright.clearAnimation();

			ivright.setAnimation(imageFadeIn);

		}

	}
}
